package Test;

//写一个泛型类 当中有一个方法 可以求数组最大值
class Person implements Comparable<Person>{
    @Override
    public int compareTo(Person o) {
        return 0;
    }
}

//第一种写法
class Alg <E extends Comparable<E>>{
    public E findMax(E[] array){
        E max=array[0];
        for (int i = 1 ;i <array.length ; i++) {
            //if(max<array[i]){}
            if(max.compareTo(array[i])<0){
                max=array[i];
            }
        }
        return max;
    }
}

//第二种写法
class Alg2{
    public <E extends Comparable<E>> E findMax(E[] array){
        E max=array[0];
        for (int i = 1 ;i <array.length ; i++) {
            //if(max<array[i]){}
            if(max.compareTo(array[i])<0){
                max=array[i];
            }
        }
        return max;
    }
}

//也可以通过静态来写
class Alg3{
    public static <E extends Comparable<E>> E findMax(E[] array){
        E max=array[0];
        for (int i = 1 ;i <array.length ; i++) {
            //if(max<array[i]){}
            if(max.compareTo(array[i])<0){
                max=array[i];
            }
        }
        return max;
    }
}
public class TestGeneric {
    public static void main1(String[] args) {
        Integer[] array={1,2,3,4,5,6,7};
        int ret=Alg3.findMax(array);
        System.out.println(ret);
    }

    public static void main2(String[] args) {
        Alg2 alg2=new Alg2();
        Integer[] array={1,2,3,4,5,6,7,8};
        int ret=alg2.findMax(array);
        System.out.println(ret);
    }

    public static void main(String[] args) {
        Integer[] array={1,2,3,4,5,9,6,8};
        Alg<Integer> alg=new Alg<Integer>();
        int a= alg.findMax(array);
        System.out.println(a);

        Alg<Person> alg23=new Alg<Person>();
    }
}
